package com.example.test_load_pic_rough;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.previewlibrary.GPreviewBuilder;
import com.previewlibrary.ZoomMediaLoader;
import com.xuexiang.xui.widget.imageview.IconImageView;

import java.util.ArrayList;
import java.util.List;

public class LoadTooLargePicByIconImageView extends AppCompatActivity {

    private final  String TAG = "LoadTooLargePicByIconImageView";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load_too_large_pic_by_icon_image_view);

        RequestOptions requestOptions  = new RequestOptions()
                .placeholder(R.drawable.image_loading)  //加载过程中占位的图片
                .error(R.drawable.image_load_error) //加载失败显示的图片
                //.fallback() //如果请求的 url/model 为 null 的时候展示的图片（如果没有设置，还是展示 placeholder 的占位符）；
                //.override(DpUtil.dp2px(context.getResources().getDimension(R.dimen.pv_msg_max_length)), DpUtil.dp2px(context.getResources().getDimension(R.dimen.pv_msg_max_hight)))//指定加载到的图片的大小 不指定频繁刷新大小会变化
                .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)//按照原图加载，而不是缩略图，如果是缩略图会导致图片模糊
                .diskCacheStrategy(DiskCacheStrategy.ALL); //设置磁盘缓存 缓存原始图片

        String filePath1 = "/storage/emulated/0/a_test_file/toolarge.jpg";
        String filePath2 = "/storage/emulated/0/a_test_file/pic.png";
        IconImageView iconImageView = findViewById(R.id.iconIv);
        //显示图片
        Glide.with(iconImageView.getContext())
                .asBitmap()
                .load(filePath1)
                //.apply(GlideMediaLoader.getRequestOptions()) //这里使用XUI自带的RequestOptions
                .apply(requestOptions)
                .override(1080,1920) //使用这个方法会改变图片的分辨率，清晰度会下降
                .transform(new RoundedCorners(15)) //设置加载到的图片的圆角
                //.centerCrop() //让图片居中放大
                .centerInside() //无论imageview大小如何，始终按照原尺寸加载图片，无论你的override设置的如何，一定会保证图片的比例是不会变的
                .dontAnimate() //取消过度动画
                .addListener(new RequestListener<Bitmap>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                        Log.e(TAG, "onLoadFailed:图片/视频加载失败,url=>" +filePath1 );
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(iconImageView);
        //点击预览图片
        iconImageView.setOnClickListener(v->{
            previewImage(filePath1);
        });



        IconImageView iconImageView2 = findViewById(R.id.iv2);
        //显示图片
        Glide.with(iconImageView.getContext())
                .asBitmap()
                .load(filePath2)
                //.apply(GlideMediaLoader.getRequestOptions()) //这里使用XUI自带的RequestOptions
                .apply(requestOptions)
                .override(1080,1920)
                .transform(new RoundedCorners(15)) //设置加载到的图片的圆角
                //.centerCrop() //让图片居中放大
                .centerInside() //无论imageview大小如何，始终按照原尺寸加载图片
                .dontAnimate() //取消过度动画
                .addListener(new RequestListener<Bitmap>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                        Log.e(TAG, "onLoadFailed:图片/视频加载失败,url=>" +filePath2 );
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(iconImageView2);
        //点击预览图片
        iconImageView2.setOnClickListener(v->{
            previewImage(filePath2);
        });

    }

    private void previewImage(String filePath) {
        ZoomMediaLoader.getInstance().init(new TestImageLoader());
        List<PicturePreviewInfo> stringList = new ArrayList<>();   //数据集合
        //stringList.add(new PicturePreviewInfo("/storage/emulated/0/a_test_file/video1.mp4" ,"/storage/emulated/0/a_test_file/video1.mp4"));
        //stringList.add(new PicturePreviewInfo("/storage/emulated/0/a_test_file/pic.png", "/storage/emulated/0/a_test_file/pic.png"));
        stringList.add(new PicturePreviewInfo("", filePath));
        //stringList.add(new UserViewInfo("/storage/emulated/0/tkthresearch/SDK/download/Screenshot_20250210-104639-xx.png"));
        GPreviewBuilder.from(LoadTooLargePicByIconImageView.this)//这里放入activity，me是我自己的命名。
                .setData(stringList)//放入数据集合
                .setCurrentIndex(0)//放入你需要查看图片的下标（通常我们使用RecyclerView来展示图片，可以在点击item时放入此代码块，这样可以实现点击查看图片的功能，下标就是为了知道当前展示的是哪一张图）
                .setSingleFling(true)//是否在黑屏区域点击返回
                .setDrag(true)//是否禁用图片拖拽返回
                .setType(GPreviewBuilder.IndicatorType.Number)//指示器类型
                .start();//启动
    }
}